package com.yxx.controller.admin;

import com.yxx.medicinecommon.constant.JwtClaimsConstant;
import com.yxx.medicinecommon.properties.JwtProperties;
import com.yxx.medicinecommon.result.PageResult;
import com.yxx.medicinecommon.result.ResponseResult;
import com.yxx.medicinecommon.utils.JwtUtil;
import com.yxx.medicinepojo.dto.EmployeeDTO;
import com.yxx.medicinepojo.dto.EmployeeLoginDTO;
import com.yxx.medicinepojo.dto.EmployeePageQueryDTO;
import com.yxx.medicinepojo.dto.PasswordEditDTO;
import com.yxx.medicinepojo.entity.Employee;
import com.yxx.medicinepojo.vo.EmployeeLoginVO;
import com.yxx.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工相关接口
 * @author yuxiangxun
 * @date 2024/9/5
 * @apiNote
 */
@Slf4j
@Api(tags = "员工相关接口")
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public ResponseResult<EmployeeLoginVO> login(
            @ApiParam(value = "员工登录信息", required = true)
            @RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO.getUsername());

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return ResponseResult.success(employeeLoginVO);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出登录")
    public ResponseResult<String> logout(){
        return ResponseResult.success();
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页")
    public ResponseResult<PageResult> PageResult(EmployeePageQueryDTO employeePageQueryDTO){
        PageResult pageResult = employeeService.page(employeePageQueryDTO);
        return ResponseResult.success(pageResult);
    }

    /**
     * 根据员工id查看员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据员工id查看员工信息")
    public ResponseResult<Employee> select_Employee(@PathVariable("id") Long id){
        Employee employee = employeeService.select_Employee_id(id);
        return ResponseResult.success(employee);
    }

    @PostMapping
    @ApiOperation("添加员工")
    public ResponseResult insert_Employee(@RequestBody EmployeeDTO employeeDTO){
        employeeService.insert(employeeDTO);
        return ResponseResult.success();
    }

    /**
     * 员工的启用与禁用
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("员工账号启用与禁用")
    public ResponseResult Enable_Disable_Status(@PathVariable("status") Integer status, Long id){
        log.info(" 员工id : {}, 启用1/禁用0 :{}",id,status);
        employeeService.enable_disable_status(status,id);
        return ResponseResult.success();
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public ResponseResult update_Employee(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update_Employee(employeeDTO);
        return ResponseResult.success();
    }

    /**
     * 修改/更新密码
     * @param passwordEditDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public ResponseResult updateEmployeePassword(@RequestBody PasswordEditDTO passwordEditDTO) throws  Exception{
        employeeService.updateEmployeePassword(passwordEditDTO);
        return ResponseResult.success();
    }
}
